//
//  RadioListViewController.m
//  Leisure
//
//  Created by xalo on 16/7/4.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "RadioListViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface RadioListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *radioListArray;
@property (nonatomic,strong)UIImageView *headerImageView;
@property (nonatomic,assign)NSInteger newStart;

@end

@implementation RadioListViewController

-(NSMutableArray *)radioListArray{
    if (!_radioListArray) {
        _radioListArray = [NSMutableArray array];
    }
    return _radioListArray;
}
-(UIImageView *)headerImageView{
    if (!_headerImageView) {
        _headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 250)];
    }
    return _headerImageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.radioModel.title;

    [self.tableView registerNib:[UINib nibWithNibName:@"RadioListCell" bundle:nil] forCellReuseIdentifier:@"RadioListModel_cell"];
    [self loadVIewWithStart:0 limit:10];
    self.tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        [self.radioListArray removeAllObjects];
        [self loadVIewWithStart:0 limit:10];
    }];
    
    self.tableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        self.newStart += 10;
 
        [self loadVIewWithStart:self.newStart limit:20];
    }];

    [RequestManager fetchDataWithUrl:kRadioURL parameter:@{@"radioid":self.radioModel.radioid} method:@"POST" completed:^(NSData *data) {
        
    } updateUI:^{
        
    }];

}

-(void)loadVIewWithStart:(NSInteger)start limit:(NSInteger)limit{
    
    
    
    [RequestManager fetchDataWithUrl:kRadioListURl parameter:@{@"radioid":self.radioModel.radioid,@"start":@(start),@"limit":@(limit)} method:@"POST" completed:^(NSData *data) {
        NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",dic);
        for (NSDictionary *dict in dic[@"data"][@"list"]) {
            RadioListModel *model = [[RadioListModel alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            [self.radioListArray addObject:model];
        }
        
        NSString *tableViewHeaderImage = dic[@"data"][@"radioInfo"][@"coverimg"];
        [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:tableViewHeaderImage]];
    } updateUI:^{
        self.tableView.tableHeaderView = self.headerImageView;
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    }];
    

    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark tableViewDelegate

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //获取model
    RadioListModel *model = self.radioListArray[indexPath.row];
    //工厂模式生产cell
    BaseCell *cell = [FactoryMode produceCellWithModel:model tableView:tableView indexPath:indexPath];
    [cell setCellWithModel:model];
    
    return cell;
    
    
}
//返回cell的个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.radioListArray.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 150;
    
}
#pragma mark tableView点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    
    DownLoadViewController *download = [DownLoadViewController createController];;
    download.model = self.radioModel;
    download.coverimg = self.radioModel.coverimg;
    download.radioListModel = self.radioListArray[indexPath.row];
    download.index = indexPath.row;
    download.dataSources = self.radioListArray;
    
    
//    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:[ NSURL URLWithString:[self.radioListArray[indexPath.row] musicUrl]]];
//    self.player = [[AVPlayer alloc]initWithPlayerItem:item];
//    
//    [self.player play];
    
    [self.navigationController pushViewController:download animated:YES];
    
    
}


@end
